<template>

  <div class="app-container">

    <!--查询表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="24">
            <el-form-item label="宠物类别">
              <el-input style="width: 100%" v-model="searchObj.name" placeholder="宠物类别名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <!--添加按钮-->
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
      <!-- 批量删除按钮 -->
      <el-button class="btn-add" size="mini" @click="batchRemove()">批量删除</el-button>
    </div>

    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dataForm" :model="sysPetType" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="宠物类别名称">
          <el-input v-model="sysPetType.type"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
      @selection-change="handleSelectionChange">

      <!-- 复选框 -->
      <el-table-column type="selection"/>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="type" label="宠物类别名称"/>
      <el-table-column prop="createTime" label="创建时间" width="230"/>
      <el-table-column prop="updateTime" label="修改时间" width="230"/>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"/>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"/>
        </template>
      </el-table-column>
    </el-table>


    <!--分页组件-->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />


  </div>

</template>

<script>

  import api from '@/api/sysPetType/sysPetType.js'

  export default {
    data() {
      return {
        // listLoading:true, //  加载中的提示语.....
        list: [],
        total: 0,
        page: 1,
        limit: 4,
        searchObj: {
          name: ''
        },
        sysPetType: {},
        dialogVisible: false,
        // 选中的数据
        selectValueData: [],
      };
    },
    created() {
      this.fetchData();
    },

    methods: {

      //分页查询
      fetchData(pageNum = 1) {
        this.page = pageNum;
        console.log(this.searchObj);
        api.getPageList(this.page, this.limit, this.searchObj).then(response => {
          // console.log(response);
          // this.listLoading=false;
          this.list = response.data.records;
          this.total = response.data.total;
          // this.limit = response.data.size;
        })
        // api.findAll().then(response => {
        //   this.list = response.data.records;
        //   this.total = response.data.total;
        // })
      },

      //触发重置
      resetData() {
        // 清空表单
        this.searchObj = {};
        // 再次刷新列表数据
        this.fetchData();
      },

      //删除
      removeDataById(id) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(response1 => {
          api.removeId(id).then(response2 => {

            this.$message.success("删除成功");
            // 刷新页面
            this.fetchData();

          });
        });
      },

      // 打开弹框
      add() {
        // 将 控制弹框的 属性设置为 true
        this.dialogVisible = true;
        // 清空表单
        this.sysPetType = {};
      },

      // 根据id去回显数据
      edit(id) {
        //1.弹框
        this.dialogVisible = true;
        //2.赋值
        api.getPetTypeById(id).then(response => {
          this.sysPetType = response.data;
        })
      },

      // 添加或者修改 具体功能
      saveOrUpdate() {
        if (this.sysPetType.id != null) {
          this.updatePetType();
        } else {
          this.addPetType();
        }
      },

      // 实现添加功能
      addPetType() {
        api.savePetType(this.sysPetType)
          .then(response => {
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            // 关闭弹框
            this.dialogVisible = false;
            // 刷新数据
            this.fetchData();

          })
      },

      updatePetType() {
        api.updatePetTypeById(this.sysPetType)
          .then(response => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            // 关闭弹框
            this.dialogVisible = false;
            // 刷新数据
            this.fetchData();
          })
      },

      //触发批量删除事件
      handleSelectionChange(selectValue) {
        // console.log(selectValue);
        this.selectValueData = selectValue;
      },

      batchRemove() {
        // 判断是否有选中select
        if (this.selectValueData.length === 0) {
          this.$message.warning('请选择要删除的记录！')
          return;
        }
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          var ids = [];
          for (var i = 0; i < this.selectValueData.length; i++) {
            var obj = this.selectValueData[i];
            // 获取id值
            var id = obj.id;
            // 将id放进到数组中
            ids.push(id);
          }
          api.bactchremoveId(ids)
            .then((response) => {
              // 提示
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              // 刷新页面
              this.fetchData();
            });
        });
      },
      showAssignAuth(row) {
        this.$router.push('/system/assignAuth?id=' + row.id + '&roleName=' + row.roleName);
      }
    }
  }
</script>

<style scoped>

</style>

